package cn.iocoder.yudao.module.forward.controller.admin.eew;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.eew.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.eew.EewDO;
import cn.iocoder.yudao.module.forward.service.eew.EewService;

@Tag(name = "管理后台 - 预警信息")
@RestController
@RequestMapping("/forward/eew")
@Validated
public class EewController {

    @Resource
    private EewService eewService;

    @PostMapping("/create")
    @Operation(summary = "创建预警信息")
    @PreAuthorize("@ss.hasPermission('forward:eew:create')")
    public CommonResult<Integer> createEew(@Valid @RequestBody EewSaveReqVO createReqVO) {
        return success(eewService.createEew(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预警信息")
    @PreAuthorize("@ss.hasPermission('forward:eew:update')")
    public CommonResult<Boolean> updateEew(@Valid @RequestBody EewSaveReqVO updateReqVO) {
        eewService.updateEew(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预警信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:eew:delete')")
    public CommonResult<Boolean> deleteEew(@RequestParam("id") Integer id) {
        eewService.deleteEew(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:eew:query')")
    public CommonResult<EewRespVO> getEew(@RequestParam("id") Integer id) {
        EewDO eew = eewService.getEew(id);
        return success(BeanUtils.toBean(eew, EewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预警信息分页")
    @PreAuthorize("@ss.hasPermission('forward:eew:query')")
    public CommonResult<PageResult<EewRespVO>> getEewPage(@Valid EewPageReqVO pageReqVO) {
        PageResult<EewDO> pageResult = eewService.getEewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出预警信息 Excel")
    @PreAuthorize("@ss.hasPermission('forward:eew:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEewExcel(@Valid EewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EewDO> list = eewService.getEewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "预警信息.xls", "数据", EewRespVO.class,
                        BeanUtils.toBean(list, EewRespVO.class));
    }

}